package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.basic.vo.ShopVo;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zj
 * @since 2022-11-29
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;

    /*
    * 店铺入驻逻辑
    * 1.校验参数
    *   非空校验 基本校验
    *   图片和店铺名涉黄涉暴力校验  调用百度ai
    *
    *   业务校验：
    *     校验店铺名是否存在
    *     店铺管理员是否存在并且已经有一个管理的店铺了
    *
    * 2.如果店铺管理员不存在
    *   保存管理员
    *   如果店铺管理员存在 但是它没有管理的店铺
    *   直接把已经存在的店铺管理员id设置到店铺里面去即可
    *
    * 3.保存店铺并得到返回的店铺id
    *
    * 4.更新店铺管理员
    *
    *
    * */
    @Override
    @Transactional
    public void settlement(Shop shop) {

        //店铺名非空检验
        if (StringUtils.isBlank(shop.getName())){//判断是否   “","   ",null
            throw new BusinessException("参数不能为空");
        }


        //校验店铺名是否存在
        Shop shopInDb = shopMapper.loadByName(shop.getName());
        if (shopInDb != null){
            throw new BusinessException("改店铺名已被注册");
        }

        //店铺管理员是否存在并且已经有一个管理的店铺了
        Employee empInDb = employeeMapper.loadByUserName(shop.getAdmin().getUsername());
        if (empInDb != null && empInDb.getShopId() != null){
            throw new BusinessException("该管理员已拥有店铺");
        }

        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名不合法，不要搞事！");
        }

        if (empInDb == null){//管理员不存在 先保存管理员

//            employeeMapper.save(shop.getAdmin()); 业务高度封装

            employeeService.add(shop.getAdmin());
            empInDb = shop.getAdmin();
        }

        shop.setAdminId(empInDb.getId());
        shopMapper.save(shop);
        empInDb.setShopId(shop.getId());
        employeeMapper.update(empInDb);


    }


    /*处理店铺审核通过的业务*/
    @Override
    @Transactional
    public void pass(ShopAuditLog log) {
        //1.修改店铺状态为已通过
        shopMapper.updateStateById(log.getShopId(),1);
        //2.把审核日志保存到数据库
        log.setAuditId(334L);//TODO 后面做了登录这里应该从当前登录用户中获取
        log.setAuditTime(new Date());
        log.setState(1);
        shopAuditLogMapper.save(log);
        //3.给用户发送激活邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();//创建一个简单邮件对象
        simpleMailMessage.setFrom("3507373533@qq.com");//从哪个邮箱发出去的
        simpleMailMessage.setTo("455818677@qq.com");//目的地邮箱
        simpleMailMessage.setSubject("激活邮件");//邮件主题
        simpleMailMessage.setText("亲，您的店铺已审核通过，请点击以下链接进行激活：http://localhost:8080/shop/active/" +  log.getShopId());//邮件内容
        javaMailSender.send(simpleMailMessage);
    }

    /*处理店铺审核驳回的业务*/
    @Override
    public void reject(ShopAuditLog log) {
        //1.修改店铺状态为失败
        shopMapper.updateStateById(log.getShopId(),-1);
        //2.把审核日志保存到数据库
        log.setAuditId(334L);//TODO 后面做了登录这里应该从当前登录用户中获取
        log.setAuditTime(new Date());
        log.setState(-1);
        shopAuditLogMapper.save(log);
        //3.给用户发送驳回邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();//创建一个简单邮件对象
        simpleMailMessage.setFrom("3507373533@qq.com");//从哪个邮箱发出去的
        simpleMailMessage.setTo("455818677@qq.com");//目的地邮箱
        simpleMailMessage.setSubject("店铺审核驳回");//邮件主题
        simpleMailMessage.setText("<h1>审核驳回</h1><br>"+
                "<span>驳回原因："+log.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+log.getShopId()+"'>点击修改资料</a>");
        javaMailSender.send(simpleMailMessage);
    }

    @Override
    public List<ShopVo> loadStateStatistics() {
        return shopMapper.loadStateStatistics();
    }


}
